package edu.mapi.ir.gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JPanel;

/**
 * @author ZP
 */
public class WaitPanel extends JPanel {

	private int numBars = 10;
	protected Area[]  ticker     = null;
	protected Thread  animation  = null;
	protected int fps = 15;
	protected long count = 0;
	protected double angleInc = Math.PI*2.0/(double)numBars;
	
	private Area buildPrimitive()
	{
		Rectangle2D.Double body = new Rectangle2D.Double(36, -6, 30, 12);
		Ellipse2D.Double   head = new Ellipse2D.Double(30, -6, 12, 12);
		Ellipse2D.Double   tail = new Ellipse2D.Double(60, -6, 12, 12);

		Area tick = new Area(body);
		tick.add(new Area(head));
		tick.add(new Area(tail));

		return tick;
	}

	public WaitPanel() {
		setOpaque(false);
		animation = new Thread(new Animator(true));
		animation.start();
	}

	@Override
	public void paint(Graphics arg0) {
		super.paint(arg0);
		Graphics2D g = (Graphics2D) arg0;
		g.setColor(new Color(228,228,228,75));
		g.fillRect(0, 0, getWidth(), getHeight());
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		Area a = buildPrimitive();
		g.translate(getWidth()/2, getHeight()/2);
		long rot = (count++) % numBars;
		g.rotate(angleInc * rot);
		
		for (int i = 0; i < numBars; i++) {
			int channel = 224 - 128 / (i + 1);
			g.setColor(new Color(channel, channel, 255, 100));
			g.fill(a);
			g.rotate(angleInc);
			
		}
		

	}

	protected class Animator implements Runnable
	{
		private boolean rampUp = true;

		protected Animator(boolean rampUp)
		{
			this.rampUp = rampUp;
		}

		public void run()
		{
		
			while (!Thread.interrupted())
			{
				repaint();

				try
				{
					Thread.sleep((1000 / fps));
				} catch (InterruptedException ie) {
					break;
				}
				Thread.yield();
			}
		}
	}

	public static void main(String[] args) {
		GuiUtils.testFrame(new WaitPanel());
	}
}
